﻿function updateClock(){
    var currentTime = new Date();

    var currentHours = currentTime.getHours ( );
    var currentMinutes = currentTime.getMinutes ( );
    var currentSeconds = currentTime.getSeconds ( );
    
    if(currentHours==0 && currentMinutes==0 && currentSeconds==0) updateDate();

    // Pad the minutes and seconds with leading zeros, if required
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

    // Choose either "AM" or "PM" as appropriate
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";

    // Convert the hours component to 12-hour format if needed
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;

    // Convert an hours component of "0" to "12"
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;

    // Compose the string for display
    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

    // Update the time display
    $('#clock').text(currentTimeString);
}

function updateDate(){
    var currentTime = new Date();
    var currentDay = currentTime.getDay();
    var currentDate = currentTime.getDate();
    var currentMonth = currentTime.getMonth();
    var currentYear = currentTime.getFullYear();
    
    var calendar = stringDay(currentDay) + ', ' + currentDate + ' ' + stringMonth(currentMonth) + ' ' + currentYear;

    // Update the date display
    $('#date').text(calendar);
}

function getTime(){
    updateDate();
    updateClock();
    setInterval('updateClock()',1000);
}

function stringDay(num){
    switch(num){
        case 0: return "Minggu";
        case 1: return "Senin";
        case 2: return "Selasa";
        case 3: return "Rabu";
        case 4: return "Kamis";
        case 5: return "Jumat";
        case 6: return "Sabtu";
    }
}

function stringMonth(num){
    switch(num){
        case 0: return "Januari";
        case 1: return "Pebruari";
        case 2: return "Maret";
        case 3: return "April";
        case 4: return "Mei";
        case 5: return "Juni";
        case 6: return "Juli";
        case 7: return "Agustus";
        case 8: return "September";
        case 9: return "Oktober";
        case 10: return "Nopember";
        case 11: return "Desember";
    }
}

